<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of CoreHTMLHelper
 *
 * @author SC_DANIEL
 */
class CoreFormHelper extends FormHelper {
    public $helpers = array('Session', 'Html'); 
    /**
     * Creates a textarea widget.
     *
     * ### Options:
     *
     * - `escape` - Whether or not the contents of the textarea should be escaped. Defaults to true.
     *
     * @param string $fieldName Name of a field, in the form "Modelname.fieldname"
     * @param array $options Array of HTML attributes, and special options above.
     * @return string A generated HTML text input element
     * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::textarea
     */
    public function ckeditor($fieldName, $options = array()) {
        Configure::write('CKEDITOR', true);

        $options['type'] = 'textarea';

        if (empty($options['id'])) {
            $options['id'] = uniqid();
        }

        $s = "  <script>
                    $(function() {

                        CKEDITOR.replace('{$options['id']}',
                        {
                                toolbar :
                                [
                                        { name: 'basicstyles', items : [ 'Bold','Italic' ] },
                                        { name: 'paragraph', items : [ 'NumberedList','BulletedList' ] },
                                        { name: 'tools', items : [ 'Maximize','-','About' ] }
                                ]
                        });
                    });
                </script>";

        return $s . $this->input($this->Session->read('currentModelName') . "." . $fieldName, $options);
    }

    public function datebox($fieldName, $options = array()) {
        if (!empty($this->request->data[$this->Session->read('currentModelName')][$fieldName])) {
            $this->request->data[$this->Session->read('currentModelName')][$fieldName] = AppController::dateMySQLToBrString($this->request->data[$this->Session->read('currentModelName')][$fieldName]);
        }
        $options['style'] = "width:100px;";
        $options['type'] = 'text';
        if (empty($options['id'])) {
            $options['id'] = uniqid();
        }

        $s = "  <script>
                    $(function() {
                        $('#{$options['id']}').datebox({ 
                                        formatter:function(date) {
                                            return formatter(date, false);
                                        },
                                        parser:function(s) {
                                            return parser(s);
                                        }
                                    });
                    });
                </script>";
        return $s . $this->input($this->Session->read('currentModelName') . "." . $fieldName, $options);
    }

    public function dateTimebox($fieldName, $options = array()) {
        if (!empty($this->request->data[$this->Session->read('currentModelName')][$fieldName])) {
            $this->request->data[$this->Session->read('currentModelName')][$fieldName] = AppController::dateMySQLToBrString($this->request->data[$this->Session->read('currentModelName')][$fieldName]);
        }
        $options['style'] = "width:170px;";
        $options['type'] = 'text';
        if (empty($options['id'])) {
            $options['id'] = uniqid();
        }

        $s = "  <script>
                    $(function() {
                        $('#{$options['id']}').datetimebox({ 
                                        showSeconds: false,
                                        formatter:function(date) {
                                            return formatter(date, true);
                                        },
                                        parser:function(s) {
                                            return parser(s);
                                        }
                                    });
                    });
                </script>";

        return $s . $this->input($this->Session->read('currentModelName') . "." . $fieldName, $options);
    }

    public function checkboxSelectAll($fieldName, $options = array()) {
        $options['type'] = 'select';
        $options['multiple'] = 'checkbox';
        $options['class'] = str_replace(".", "_", $fieldName);
        $id = str_replace(" ", "", ucwords(str_replace("_", " ", $options['class'])));

        $select = array('0' => __('Select All'));
        $options['options'] = $select + $options['options'];
        $s = "<script>   
                    $(function() {
                        $('#{$id}0').click(
                            function() {
                                $('.{$options['class']} input').each(function() {
                                    if($(this).attr('id') != '{$id}0'){
                                        $(this).prop('checked', $('#{$id}0').prop('checked'));
                                    }
                                });
                            }
                        );
                    });
                </script>";

        return $s . $this->input($fieldName, $options);
    }

}

?>